package com.weilai.dao;

import com.weilai.domain.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface VideosDao {
    //按id查询
    @Select("select * from videos where video_id=#{id}")
    Videos selectById(int id);
    //按userId查询
    @Select("select * from videos where user_id=#{id}")
    List<Videos> selectByUserId(int id);
    // 添加视频描述信息
    @Insert("INSERT INTO videos (user_id, title, description, tag_id,file_path) " +
            "VALUES (#{userId}, #{title}, #{description}, #{tagId},#{filePath})")
    void addDescription(Videos video);

    // 添加视频文件信息
    @Insert("INSERT INTO videos (user_id, file_path,upload_time, view_count, like_count, comment_count, share_count) " +
            "VALUES (#{userId}, #{filePath}, #{uploadTime}, 0, 0, 0, 0)")
    void addFile(Videos video);
    //删除视频
    @Delete("delete from videos where video_id=#{id}")
    void deleteById(int id);
    //修改视频
    @Update("update videos set title=#{title},description=#{description},file_path={filePath} where video_id=#{videoId}")
    void update(Videos video);
    //查询所有视频
    @Select("select * from videos order by upload_time desc")
    List<Videos> selectAllVideos();
    //查询别人所有视频
    @Select({"select * from videos where user_id=#{userId} order by upload_time desc"})
    List<Videos> selectAllUsersVideos(int userId);
    //查询自己所有视频
    @Select({"select * from videos where user_id=#{userId} order by upload_time desc"})
    List<Videos> selectMyAllVideos(int userId);
    //根据标题关键字查询视频
    @Select({"select * from videos where title like '%#{title}%'"})
    List<Videos> selectByTitle(String title);
    //根据时间查询视频
    @Select({"select * from videos where upload_time between #{date1} and #{date2}"})
    List<Videos> selectByDate(String date1, String date2);
    // 点赞数增加1
    @Update("update videos set like_count = like_count + 1 where video_id = #{videoId}")
    void incrementLikeCount(int videoId);
    // 评论数增加1
    @Update("update videos set comment_count = comment_count + 1 where video_id = #{videoId}")
    void incrementCommentCount(int videoId);
    // 浏览数增加1
    @Update("update videos set view_count = view_count + 1 where video_id = #{videoId}")
    void incrementViewCount(int videoId);
    // 转发数增加1
    @Update("update videos set share_count = share_count + 1 where video_id = #{videoId}")
    void incrementShareCount(int videoId);
    // 插入评论记录
    @Insert("insert into comments (user_id, video_id, content,created_at) values (#{userId}, #{videoId}, #{content},#{createdAt})")
    void insertCommentRecord(Comments comments);
    // 查询指定视频的所有评论
    @Select("select * from comments where video_id=#{videoId} order by created_at desc")
    List<Comments> getCommentsByVideoId(int videoId);
    // 插入点赞记录
    @Insert("insert into likes (user_id, video_id) values (#{userId}, #{videoId})")
    void insertLikeRecord(int userId, int videoId);
    // 检查用户是否已点赞
    @Select("select count(*) from likes where user_id = #{userId} and video_id = #{videoId} and is_deleted = 0")
    int countUserLiked(int userId, int videoId);
    // 根据用户id和视频id查询点赞记录
    @Select("select * from likes where user_id = #{userId} and video_id = #{videoId} and is_deleted = 0")
    Likes selectLikeRecord(int userId, int videoId);
    // 更新点赞记录为已取消（将is_deleted设为1 ）
    @Update("update likes set is_deleted = 1 where id = #{likeId} and video_id=#{videoId}")
    void cancelLikeRecord(int likeId,int videoId);
    // 点赞数减少1
    @Update("update videos set like_count = like_count - 1 where video_id = #{videoId}")
    void decrementLikeCount(int videoId);
    @Delete("delete from likes where user_id=#{userId} and video_id=#{videoId} and is_deleted=1")
    void delete (int userId,int videoId);
    //查询点赞数
    @Select("select like_count from videos where video_id=#{videoId}")
    int likeCount(int videoId);
    // 根据评论id和用户id删除评论
    @Delete("DELETE FROM comments WHERE id = #{commentId} AND user_id = #{userId}")
    int deleteCommentByUser(int commentId, int userId);
    // 根据评论id和视频作者id删除评论
    @Delete("DELETE FROM comments WHERE id = #{commentId} AND video_id IN" +
            " (SELECT id FROM videos WHERE user_id = #{userId})")
    int deleteCommentByAuthor(int commentId, int userId);
    @Select("SELECT id, content, user_id as userId, video_id as videoId, " +
            "created_at as createdAt " +
            "FROM comments WHERE id = #{commentId}")
    Comments getCommentById(int commentId);
    // 根据评论id获取所属视频id
    @Select("SELECT video_id FROM comments WHERE id = #{commentId}")
    int getVideoIdByCommentId(int commentId);
    // 评论数减1
    @Update("UPDATE videos SET comment_count = comment_count - 1 WHERE video_id = #{videoId}")
    void decrementCommentCount(int videoId);
    @Select("select count(*) from comments where video_id=#{id}")
     int selectCommentCount(int id);
    //在首页根据视频标题关键字查询
    @Select("select * from videos where title like concat('%',#{keywords},'%')")
    List<Videos> selectByKeywords(String keywords);

    // 按 userid 删除视频
    @Delete("DELETE FROM videos WHERE user_id = #{userId}")
    void deleteUserById(int userId);

    //按userId查询视频数据
    @Select("select view_count, like_count, comment_count, share_count from videos where user_id=#{userId}")
    List<Videos> selectData(int userId);
//    //按id查询标签id
//    @Select("select id from tag where tag_name=#{name}")
//    int selectTagById(String name);
    // 根据标签名称查询
    @Select("select * from tag where tag_name=#{tagName}")
    Tag findByTagName(String tagName);
    // 根据ID查询
    @Select("select * from tag where id=#{id}")
    Tag getById(Integer id);
}
